<template>
  <div>
    <div class="top-panel">
      <el-form :model="searchFormData" label-width="50px">
        <el-row>
          <el-col :span="4">
            <el-form-item label="昵称" prop="nickNameFuzzy">
              <el-input
                placeholder="请输入昵称"
                v-model="searchFormData.nickNameFuzzy"
                clearable
                @keyup.enter.native="loadDataList"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="性别" prop="sex">
              <el-select
                v-model="searchFormData.sex"
                clearable
                placeholder="请选择性别"
                :style="{ width: '100%' }"
              >
                <el-option :value="1" label="男"></el-option>
                <el-option :value="0" label="女"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="状态" prop="status">
              <el-select
                v-model="searchFormData.status"
                clearable
                placeholder="请选择状态"
                :style="{ width: '100%' }"
              >
                <el-option :value="1" label="正常"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="5" :style="{ 'padding-left': '10px' }">
            <el-button type="primary" @click="loadDataList">搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="data-list">
      <Table
        :columns="columns"
        :showPagination="true"
        :dataSource="tableData"
        :fetch="loadDataList"
        :options="tableOptions"
      >
        <!--头像-->
        <template #avatar="{ row }">
          <Avatar :userId="row.userId"></Avatar>
        </template>
        <!--昵称-->
        <template #nickName="{ row }">
          <span v-if="row.nickName">{{ row.nickName }}</span>
        </template>
        <!--邮箱-->
        <template #email="{ row }">
          <span>{{ row.email }}</span>
        </template>
        <!--登录信息-->
        <template #loginInfo="{ row }">
          <div>最后登录时间:{{ row.lastLoginTime }}</div>
          <div>最后登录IP:{{ row.lastLoginIp }}</div>
          <div>最后登录地点:{{ row.lastLoginIpAddress }}</div>
        </template>
        <!--积分-->
        <template #integral="{ row }">
          <div>总积分:{{ row.totalIntegral }}</div>
          <div>当前积分:{{ row.currentIntegral }}</div>
        </template>
        <!--状态-->
        <template #status="{ row }">
          <span v-if="row.status == 1" :style="{ color: 'green' }">启用</span>
          <span v-if="row.status == 0" :style="{ color: 'red' }">禁用</span>
        </template>
        <!--操作-->
        <template #op="{ row }">
          <div class="op" v-if="row.status != -1">
            <el-dropdown trigger="click">
              <span class="iconfont icon-more"></span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="updateUserStatus(row)"
                    ><span v-has="proxy.PermissionCode.user.user_op_status">{{
                      row.status == 1 ? "禁用" : "启用"
                    }}</span></el-dropdown-item
                  >
                  <el-dropdown-item @click="sendMessage(row)"
                    ><span v-has="proxy.PermissionCode.user.user_send_message"
                      >发送系统消息</span
                    ></el-dropdown-item
                  >
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </template>
      </Table>
    </div>
    <!-- 监听 SendMessage 组件的 reload 事件 -->
    <SendMessage ref="sendMessageRef" @reload="loadDataList"></SendMessage>
  </div>
</template>

<script setup>
import SendMessage from "./SendMessage.vue";
import { ref, reactive, getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance();

const api = {
  loadDataList: "/user/loadUserList",
  updateUserStatus: "/user/updateUserStatus",
};

const searchFormData = ref({});

//列表
const columns = [
  {
    label: "头像",
    prop: "avatar",
    width: 80,
    scopedSlots: "avatar",
  },
  {
    label: "昵称",
    prop: "nickName",
    width: 150,
    scopedSlots: "nickName",
  },
  {
    label: "邮箱",
    prop: "email",
    width: 210,
  },
  {
    label: "个人描述",
    prop: "personDescription",
  },
  {
    label: "加入时间",
    prop: "joinTime",
    width: 165,
  },
  {
    label: "登录信息",
    prop: "lastLoginTime",
    width: 250,
    scopedSlots: "loginInfo",
  },
  {
    label: "积分",
    prop: "totalIntegral",
    width: 120,
    scopedSlots: "integral",
  },
  {
    label: "状态",
    prop: "status",
    scopedSlots: "status",
    width: 55,
  },
  {
    label: "操作",
    prop: "op",
    width: 60,
    scopedSlots: "op",
  },
];

const tableData = ref({});
const tableOptions = {
  extHeight: 50,
};

const loadDataList = async () => {
  let params = {
    pageNo: tableData.value.pageNo,
    pageSize: tableData.value.pageSize,
  };

  Object.assign(params, searchFormData.value);
  let result = await proxy.Request({
    url: api.loadDataList,
    params: params,
  });
  if (!result) {
    return;
  }
  tableData.value = result.data;
};

//禁用启用用户
const updateUserStatus = (data) => {
  const title = data.status == 1 ? "禁用" : "启用";
  proxy.Confirm(`你确定要${title}用户${data.nickName}吗？`, async () => {
    let result = await proxy.Request({
      url: api.updateUserStatus,
      params: {
        userId: data.userId,
        status: data.status == 1 ? 0 : 1,
      },
    });
    if (!result) {
      return;
    }
    loadDataList();
  });
};

//发送系统消息
const sendMessageRef = ref();
const sendMessage = (row) => {
  sendMessageRef.value.sendMessage(row);
};
</script>

<style lang="scss" scoped>
.op {
  cursor: pointer;
}
</style>
